home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / TransSkel / Demos / Pascal Demos / DialogSkel / DialogSkel.p next >
Encoding:
Text File  |  1994-02-23  |  9.3 KB  |  445 lines  |  [TEXT/PJMM]

  1. { TransSkel DialogSkel application in Pascal }
  2.  
  3. { 11 Feb 94 Version 1.00, Paul DuBois }
  4.  
  5. program DialogSkel;
  6.  
  7.     uses
  8.         TransSkel;
  9.  
  10.     const
  11.  
  12.         fileMenuID = skelAppleMenuID + 1;
  13.         editMenuID = fileMenuID + 1;
  14.  
  15.         mDlogRes = 1000;
  16.         aboutAlrtRes = 1001;
  17.         docWindRes = 1000;
  18.  
  19.         showDlog1 = 1;
  20.         showDlog2 = 2;
  21.         showDoc = 3;
  22.         closeWind = 4;
  23. { separator line }
  24.         quit = 6;
  25.  
  26.         undo = 1;
  27. { separator line }
  28.         cut = 3;
  29.         copy = 4;
  30.         paste = 5;
  31.         clear = 6;
  32.  
  33.         button1 = 1;
  34.         edit1 = 2;
  35.         static1 = 3;
  36.         radio1 = 4;
  37.         radio2 = 5;
  38.         radio3 = 6;
  39.         check1 = 7;
  40.         check2 = 8;
  41.         user1 = 9;
  42.  
  43.     var
  44.  
  45.         mDlog1: DialogPtr;
  46.         mDlog2: DialogPtr;
  47.         docWind: WindowPtr;
  48.         iconNum1: Integer;
  49.         iconNum2: Integer;
  50.  
  51.         fileMenu: MenuHandle;
  52.         editMenu: MenuHandle;
  53.  
  54.         hidden1: Boolean;            { flags for keeping track of dialog }
  55.         hidden2: Boolean;            { visibility when a suspend occurs }
  56.  
  57. {--------------------------------------------------------------------}
  58. { Miscellaneous stuff }
  59. {--------------------------------------------------------------------}
  60.  
  61.     procedure DrawIcon (dlog: DialogPtr;
  62.                                     item: Integer);
  63.         var
  64.             h: Handle;
  65.             r: Rect;
  66.     begin
  67.         SkelGetDlogRect(dlog, item, r);
  68.         if (dlog = mDlog1) then
  69.             h := GetIcon(iconNum1)
  70.         else
  71.             h := GetIcon(iconNum2);
  72.         PlotIcon(r, h);
  73.     end;
  74.  
  75.  
  76.     procedure SetDlogRadio (dlog: DialogPtr;
  77.                                     item: Integer);
  78.         var
  79.             partner: DialogPtr;
  80.             tmpPort: GrafPtr;
  81.             r: Rect;
  82.     begin
  83.         partner := DialogPtr(GetWRefCon(dlog));
  84.         SkelSetDlogRadioButtonSet(dlog, radio1, radio3, item);
  85.  
  86.         if (partner = mDlog1) then
  87.             iconNum1 := item - radio1
  88.         else
  89.             iconNum2 := item - radio1;
  90.  
  91.         SkelGetDlogRect(partner, user1, r);
  92.         GetPort(tmpPort);
  93.         SetPort(partner);
  94.         InvalRect(r);        { invalidate item rect to generate update rect }
  95.         SetPort(tmpPort);
  96.     end;
  97.  
  98.  
  99. {--------------------------------------------------------------------}
  100. { Dialog window setup and handler routines }
  101. {--------------------------------------------------------------------}
  102.  
  103.  
  104.     procedure DlogEvent (item: Integer;
  105.                                     event: EventRecord);
  106.         var
  107.             actor: DialogPtr;
  108.             partner: DialogPtr;
  109.             title: Str255;
  110.             value: Integer;
  111.     begin
  112.         GetPort(actor);
  113.         partner := DialogPtr(GetWRefCon(actor));
  114.         case item of
  115.             button1: 
  116.                 begin
  117.                     SkelGetDlogStr(actor, edit1, title);
  118.                     SetWTitle(partner, title);
  119.                 end;
  120.             radio1, radio2, radio3: 
  121.                 SetDlogRadio(actor, item);
  122.             check1: 
  123.                 begin
  124.                     value := SkelToggleDlogCtlValue(actor, item);
  125.                     if (value = 0) then
  126.                         HideWindow(partner)
  127.                     else
  128.                         ShowWindow(partner);
  129.                 end;
  130.             check2: 
  131.                 begin
  132.                     value := SkelToggleDlogCtlValue(actor, item);
  133.                     if (value <> 0) then
  134.                         WindowPeek(partner)^.goAwayFlag := Boolean(255)
  135.                     else
  136.                         WindowPeek(partner)^.goAwayFlag := Boolean(0);
  137.                 end;
  138.         end;
  139.     end;
  140.  
  141.  
  142.     procedure DlogClose;
  143.         var
  144.             actor: DialogPtr;
  145.             partner: DialogPtr;
  146.     begin
  147.         GetPort(actor);
  148.         partner := DialogPtr(GetWRefCon(actor));
  149.         HideWindow(actor);
  150.         SkelSetDlogCtlValue(partner, check1, 0);
  151.     end;
  152.  
  153.  
  154.     procedure DlogClobber;
  155.         var
  156.             dlog: DialogPtr;
  157.     begin
  158.         GetPort(dlog);
  159.         DisposeDialog(dlog);
  160.     end;
  161.  
  162.  
  163.     function DemoDialog (title: Str255;
  164.                                     h: Integer;
  165.                                     v: Integer): DialogPtr;
  166.         var
  167.             dlog: DialogPtr;
  168.             ignore: Boolean;
  169.     begin
  170.         dlog := GetNewDialog(mDlogRes, nil, WindowPtr(-1));
  171.         MoveWindow(dlog, h, v, false);
  172.         SetWTitle(dlog, title);
  173.         ignore := SkelDialog(dlog, @DlogEvent, @DlogClose, @DlogClobber);
  174.         DemoDialog := dlog;
  175.     end;
  176.  
  177.  
  178. {--------------------------------------------------------------------}
  179. { Document window setup and handler routines }
  180. {--------------------------------------------------------------------}
  181.  
  182.  
  183.     procedure DocUpdate (resized: Boolean);
  184.     begin
  185.     end;
  186.  
  187.  
  188.     procedure DocActivate (active: Boolean);
  189.     begin
  190.     end;
  191.  
  192.  
  193.     procedure DocClobber;
  194.     begin
  195.         HideWindow(docWind);
  196.         DisposeWindow(docWind);
  197.     end;
  198.  
  199.  
  200.     procedure DocWindow (h: Integer;
  201.                                     v: Integer);
  202.         var
  203.             ignore: Boolean;
  204.     begin
  205.         if (SkelQuery(skelQHasColorQD) <> 0) then
  206.             docWind := GetNewCWindow(docWindRes, nil, WindowPtr(-1))
  207.         else
  208.             docWind := GetNewWindow(docWindRes, nil, WindowPtr(-1));
  209.         ignore := SkelWindow(docWind, nil, nil, @DocUpdate, @DocActivate, nil, @DocClobber, nil, false);
  210.         MoveWindow(docWind, h, v, false);
  211.     end;
  212.  
  213.  
  214. {--------------------------------------------------------------------}
  215. { Menu handlers }
  216. {--------------------------------------------------------------------}
  217.  
  218.  
  219. { Handle selection of "About Button..." item from Apple menu }
  220.  
  221.     procedure DoAppleMenu (item: Integer);
  222.         var
  223.             ignore: Integer;
  224.     begin
  225.         ignore := SkelAlert(aboutAlrtRes, SkelDlogFilter(nil, true), skelPositionOnParentDevice);
  226.         SkelRmveDlogFilter;
  227.     end;
  228.  
  229.  
  230. { Process selection from File menu }
  231.  
  232.     procedure DoFileMenu (item: Integer);
  233.     begin
  234.         case item of
  235.             showDlog1: 
  236.                 begin
  237.                     SelectWindow(mDlog1);
  238.                     ShowWindow(mDlog1);
  239.                     SkelSetDlogCtlValue(mDlog2, check1, 1);
  240.                 end;
  241.             showDlog2: 
  242.                 begin
  243.                     SelectWindow(mDlog2);
  244.                     ShowWindow(mDlog2);
  245.                     SkelSetDlogCtlValue(mDlog1, check1, 1);
  246.                 end;
  247.             showDoc: 
  248.                 begin
  249.                     SelectWindow(docWind);
  250.                     ShowWindow(docWind);
  251.                 end;
  252.             closeWind: 
  253.                 SkelClose(FrontWindow);
  254.             quit: 
  255.                 SkelStopEventLoop;
  256.         end;
  257.     end;
  258.  
  259.  
  260.     procedure DoEditMenu (item: Integer);
  261.         var
  262.             dlog: DialogPtr;
  263.             ignore: Integer;
  264.     begin
  265.         if (SystemEdit(item - 1)) then    { if DA handled operation, return }
  266.             exit(DoEditMenu);
  267.  
  268. { if front window is document window, do nothing }
  269.         dlog := DialogPtr(FrontWindow);
  270.         if (WindowPeek(dlog)^.windowKind <> dialogKind) then
  271.             exit(DoEditMenu);
  272.         case item of
  273.             cut: 
  274.                 begin
  275.                     DlgCut(dlog);
  276.                     ignore := ZeroScrap;
  277.                     ignore := TEToScrap;
  278.                 end;
  279.             copy: 
  280.                 begin
  281.                     DlgCopy(dlog);
  282.                     ignore := ZeroScrap;
  283.                     ignore := TEToScrap;
  284.                 end;
  285.             paste: 
  286.                 begin
  287.                     ignore := TEFromScrap;
  288.                     DlgPaste(dlog);
  289.                 end;
  290.             clear: 
  291.                 DlgDelete(dlog);
  292.         end;
  293.     end;
  294.  
  295.  
  296. { Adjust menus when mouse click occurs in menu bar, }
  297. { before menus are shown. }
  298.  
  299.     procedure AdjustMenus;
  300.         var
  301.             w: WindowPtr;
  302.     begin
  303.         w := FrontWindow;
  304.         if (w = nil) then
  305.             DisableItem(fileMenu, closeWind)
  306.         else
  307.             EnableItem(fileMenu, closeWind);
  308.         if (w = docWind) then
  309.             begin
  310.                 DisableItem(editMenu, undo);
  311.                 DisableItem(editMenu, cut);
  312.                 DisableItem(editMenu, copy);
  313.                 DisableItem(editMenu, paste);
  314.                 DisableItem(editMenu, clear);
  315.             end
  316.         else
  317.             begin
  318.                 { modeless dialog or DA -- dim undo for dialogs }
  319.                 if (WindowPeek(w)^.windowKind = dialogKind) then
  320.                     DisableItem(editMenu, undo)
  321.                 else
  322.                     EnableItem(editMenu, undo);
  323.  
  324.                 EnableItem(editMenu, cut);
  325.                 EnableItem(editMenu, copy);
  326.                 EnableItem(editMenu, paste);
  327.                 EnableItem(editMenu, clear);
  328.             end;
  329.     end;
  330.  
  331.  
  332. {--------------------------------------------------------------------}
  333. { Suspend/resume handler }
  334. {--------------------------------------------------------------------}
  335.  
  336.  
  337.     procedure SuspendResume (inForeground: Boolean);
  338.         var
  339.             w: WindowPtr;
  340.             event: EventRecord;
  341.             ignore: Boolean;
  342.     begin
  343.         if (inForeground = false) then
  344.             begin
  345.                 hidden1 := false;
  346.                 hidden2 := false;
  347.                 if (WindowPeek(mDlog1)^.visible) then
  348.                     begin
  349.                         ShowHide(mDlog1, false);
  350.                         hidden1 := true;
  351.                     end;
  352.                 if (WindowPeek(mDlog2)^.visible) then
  353.                     begin
  354.                         ShowHide(mDlog2, false);
  355.                         hidden2 := true;
  356.                     end;
  357.                 w := FrontWindow;
  358.                 if (w <> nil) then
  359.                     begin
  360.                         HiliteWindow(w, false);
  361.                         SkelActivate(w, false);
  362.                     end;
  363.             end
  364.         else
  365.             begin
  366.                 w := FrontWindow;
  367.                 if (w <> nil) then
  368.                     HiliteWindow(w, false);
  369.                 if (hidden1) then
  370.                     ShowHide(mDlog1, true);
  371.                 if (hidden2) then
  372.                     ShowHide(mDlog2, true);
  373.                 w := FrontWindow;
  374.                 if (w <> nil) then
  375.                     begin
  376.                         HiliteWindow(w, true);
  377.                         SkelActivate(w, true);
  378.                     end;
  379.                 if (EventAvail(mDownMask, event)) then
  380.                     ignore := GetNextEvent(mDownMask, event);
  381.             end;
  382.     end;
  383.  
  384.  
  385. {--------------------------------------------------------------------}
  386. { Main program }
  387. {--------------------------------------------------------------------}
  388.  
  389.  
  390.     procedure Initialize;
  391.         var
  392.             ignore: Boolean;
  393.     begin
  394.         iconNum1 := 0;
  395.         iconNum2 := 0;
  396.  
  397.         SkelInit(nil);
  398.  
  399.         SkelSetSuspendResume(@SuspendResume);
  400.  
  401.         SkelApple('About DialogSkel…', @DoAppleMenu);
  402.  
  403.         fileMenu := NewMenu(fileMenuID, 'File');
  404.         AppendMenu(fileMenu, 'Show Dialog 1;Show Dialog 2;Show Doc Window');
  405.         AppendMenu(fileMenu, 'Close/W;(-;Quit/Q');
  406.         ignore := SkelMenu(fileMenu, @DoFileMenu, nil, false, false);
  407.  
  408.         editMenu := NewMenu(editMenuID, 'Edit');
  409.         AppendMenu(editMenu, '(Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear');
  410.         ignore := SkelMenu(editMenu, @DoEditMenu, nil, false, false);
  411.  
  412.         DrawMenuBar;
  413.         SkelSetMenuHook(@AdjustMenus);
  414.  
  415.         DocWindow(100, 125);
  416.  
  417.         mDlog1 := DemoDialog('Modeless Dialog 1', 50, 50);
  418.         mDlog2 := DemoDialog('Modeless Dialog 2', 150, 200);
  419.         SetWRefCon(mDlog1, LongInt(mDlog2));
  420.         SetWRefCon(mDlog2, LongInt(mDlog1));
  421.         SkelSetDlogStr(mDlog1, edit1, 'Modeless Dialog 2');
  422.         SkelSetDlogStr(mDlog2, edit1, 'Modeless Dialog 1');
  423.         SkelSetDlogProc(mDlog1, user1, @DrawIcon);
  424.         SkelSetDlogProc(mDlog2, user1, @DrawIcon);
  425.         SkelSetDlogRadioButtonSet(mDlog1, radio1, radio3, radio1);
  426.         SkelSetDlogRadioButtonSet(mDlog2, radio1, radio3, radio1);
  427.         SkelSetDlogCtlValue(mDlog1, check1, 1);
  428.         SkelSetDlogCtlValue(mDlog2, check1, 1);
  429.         SkelSetDlogCtlValue(mDlog1, check2, 1);
  430.         SkelSetDlogCtlValue(mDlog2, check2, 1);
  431.  
  432.         SelectWindow(docWind);
  433.         ShowWindow(docWind);
  434.         SelectWindow(mDlog1);
  435.         ShowWindow(mDlog1);
  436.         SelectWindow(mDlog2);
  437.         ShowWindow(mDlog2);
  438.     end;
  439.  
  440.  
  441. begin
  442.     Initialize;
  443.     SkelEventLoop;
  444.     SkelCleanup;
  445. end.